#ifndef _SUB_DEVICE_H
#define _SUB_DEVICE_H
#include "gd32e23x.h"
#define RF_CMD_SEND_TIME          300
#define UNLIMIT_SEND_TIME         0xFFFFFFFF

#define  GATEWAY_PAIR_BUTTON_NUM_MAX      10


#define NO_SEARCH_BIND                                0
#define WAIT_ADDSUB_ACK_FROM_CLOUD_BY_SEARCH                    1   //wait cloud tell gateway allows it add subdev(subdev_manage.if_subdev_net_in==1)
#define WAIT_SUBDEV_DATA_IN_SEARCH                       2
#define WAIT_SUBDEV_BIND_DATA                         3
#define WAIT_SUBDEV_BIND_DATA_NOT_BY_SEARCH           4
#define WAIT_CLOUD_TELL_ADD_RESULT_BY_SEARCH          5 //gateway has send add subdev cmd to cloud,it is waitting cloud's answer
#define WAIT_CLOUD_TELL_ADD_RESULT_NOT_BY_SEARCH      6
#define WAIT_ADDSUB_ACK_FROM_CLOUD_BY_KEY                    7
#define WAIT_ADDSUB_ACK_FROM_CLOUD_ENERGE_BUTTON      8
#define WAIT_ADDSUB_ACK_FROM_CLOUD_ENERGE_BUTTON_IN_SEARCH      9

typedef struct
{
   union
   {
      struct
      {
         uint8_t music_item;
         uint8_t vol;
         uint8_t reserve[2];
         struct
         {
            uint16_t pair_id;
            uint8_t  music_item;
            uint8_t reserve;
         }pair_button[GATEWAY_PAIR_BUTTON_NUM_MAX];
      }flash;
      uint32_t aligned;
   }u;
   uint8_t pair_flag;  //1:in pair mode   0:not in
   volatile uint16_t pair_filter;
   uint8_t first_connected;
   uint8_t power_on_flag;
   
}gateway_struct;

extern gateway_struct gateway;
extern uint8_t  search_bind_flag;

void fmc_read(uint32_t address, uint32_t *data, uint16_t len);
void fmc_program(uint32_t address, uint32_t *data, uint16_t len);
void fmc_erase_pages(uint32_t Addr);
int StringToHex(char *str, unsigned char *out, unsigned int *outlen);
void report_sub_dev_status(uint8_t *pt);
uint8_t get_download_cmd_cnt(void);
uint8_t gateway_handle(uint8_t dpid,const uint8_t value[], uint16_t length);
uint8_t record_joinin_net_success_subdev_in_flash(void);
void read_sub_dev_flash(void);
uint8_t remove_sub_dev_from_bind_flash(uint32_t id, uint8_t flag);
uint8_t sub_dev_dp_handle(uint8_t dpid,const uint8_t value[], uint16_t length,uint8_t* sub_id);
void sub_dev_timing_int_handler(void);
void gateway_event_handle_mainloop(void);
void subdev_out_search_bind_mode(void);
uint32_t HexDataSwitchPosition(uint32_t t);
uint8_t is_id_equal(uint8_t *id_buf);
void read_unbind_sub_dev_flash(void);
uint8_t subdev_onoff_line_judge(uint8_t *ptr_id);
void gateway_remove(void);
void factory_reset_by_key(void);
void factory_reset_by_app(void);
void wait_poll_all_subdev_status_for_sync(void);
void new_sub_dev_input_handle(uint8_t *ptr);
void new_subdev_query_mainloop(void);
void new_start_subdev_unbind(uint8_t *sub_id, uint32_t time);
uint8_t remove_joinnet_success_subdev_from_unbind_to_bind_flash(void);
void new_start_subdev_searching_or_abort(void);
void read_chip_id(void);
void goto_boot(void);
void read_gateway_from_flash(void);
void record_gateway_in_flash(void);
uint8_t gateway_pair_button_input_handle(uint8_t *ptr);
uint8_t is_gateway_pair_button_full(void);
void clean_gateway_pair_button(void);
uint8_t cancle_add_subdev(void);
void button_sub_dev_input_handle(uint8_t *ptr);
void handle_subdev_timing_task(void);


#endif

